package com.example.smartcitydemo.ui.person

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.MyApplication
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.ModifyPassword
import com.example.smartcitydemo.databinding.FragmentModifyPasswordBinding
import com.example.smartcitydemo.exception.AppExcpetion
import com.example.smartcitydemo.receiver.LoginBroadCastReceiver
import com.example.smartcitydemo.util.onBackPressed
import com.example.smartcitydemo.viewModel.person.PersonViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.person
 * @NAME: Caleb.F
 * @DATE: 2021/11/11 15:47
 * @Email: 1919796283@qq.com
 */
class ModifyPassWordFragment :
    BaseFragment<FragmentModifyPasswordBinding>(R.layout.fragment_modify_password) {
    val personViewModel: PersonViewModel by viewModels()
    override fun initData() {

    }

    override fun initView(bindView: FragmentModifyPasswordBinding, savedInstanceState: Bundle?) {
        bindView.run {
            modifyToolbar.tvContent.text = "修改密码"
            button3.setOnClickListener {
                val text1 = textInputLayout.editText?.text?.trim().toString()
                val text2 = textInputLayout2.editText?.text?.trim().toString()
                if (text1.isEmpty()) {
                    ToastUtils.showShort("请输入原密码")
                } else if (text2.isEmpty()) {
                    ToastUtils.showShort("请输入新密码")
                } else {
                    personViewModel.modifyPassWord(ModifyPassword(text2, text1))
                }
            }
        }
    }

    override fun addObservers(bindView: FragmentModifyPasswordBinding) {
        personViewModel.modifyPassWordResult.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                this.requireActivity().onBackPressed()
                val intent =
                    Intent(LoginBroadCastReceiver.LOGIN_RECEIVER_ACTION)
                intent.setPackage(MyApplication.instance.packageName)
                MyApplication.instance.sendBroadcast(intent)
            } else if (it is Resource.Failure) {
                ToastUtils.showShort("${it.msg}")
            }
        }
    }

}